package android.game;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.SharedPreferences;

import framework.FileIO;

public class Settings {
	
    public static boolean soundEnabled = true;
    public static int[] highscores = new int[] { 100, 80, 50, 30, 10 };
    public static void load(FileIO files) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(
                    files.readFile(".android.game")));
            soundEnabled = Boolean.parseBoolean(in.readLine());
            for (int i = 0; i < 5; i++) {
                highscores[i] = Integer.parseInt(in.readLine());
            }
        } catch (IOException e) {
            // :( It's ok we have defaults
        } catch (NumberFormatException e) {
            // :/ It's ok, defaults save our day
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }

    public static void save(FileIO files) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    files.writeFile(".android.game")));
            out.write(Boolean.toString(soundEnabled));
            out.write("\n");
            for (int i = 0; i < 5; i++) {
                out.write(Integer.toString(highscores[i]));
                out.write("\n");
            }

        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

    public static void addScore(int score) {
        for (int i = 0; i < 5; i++) {
            if (highscores[i] < score) {
                for (int j = 4; j > i; j--)
                    highscores[j] = highscores[j - 1];
                highscores[i] = score;
                break;
            }
        }
    }
//    public void setData(int score){
//    	SharedPreferences mySharedPreferences = getSharedPreferences(PREFERENCE_NAME,Activity.MODE_PRIVATE);
//    	SharedPreferences.Editor editor = mySharedPreferences.edit();
//    	for (int i = 0; i < 5; i++) {
//            if (highscores[i] < score) {
//                for (int j = 4; j > i; j--)
//                    highscores[j] = highscores[j - 1];
//                highscores[i] = score;
//                break;
//            }
//        }
//    	editor.putInt("first", highscores[0]);
//    	editor.putInt("second", highscores[1]);
//    	editor.putInt("third", highscores[2]);
//    	editor.putInt("fourth", highscores[3]);
//    	editor.putInt("fifth", highscores[4]);
//    	editor.commit();
//
//    }
//    public void getData(){
//    	SharedPreferences mySharedPreferences = getSharedPreferences(PREFERENCE_NAME,Activity.MODE_PRIVATE);
//    	highscores[0] = mySharedPreferences.getInt("first", 100);
//    	highscores[1] = mySharedPreferences.getInt("second", 100);
//    	highscores[2] = mySharedPreferences.getInt("third", 100);
//    	highscores[3] = mySharedPreferences.getInt("fourth", 100);
//    	highscores[4] = mySharedPreferences.getInt("fifth", 100);
//    }
    
}
